package com.fmi.cloud.act;

import android.app.Activity;
import android.os.Handler;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.fmi.cloud.R;
import com.fmi.cloud.app.YcApp;
import com.fmi.cloud.biz.CommonSp;
import com.fmi.cloud.biz.FmHttpBiz;
import com.fmi.cloud.guide.GuideFragmentActivity;
import com.fmi.cloud.util.IntentHelper;

public class SplashActivity extends Activity {

    ImageView ivSplash;
    TextView tvCountingDown;
    Handler timer;
    int count = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);

        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        ivSplash = (ImageView) findViewById(R.id.ivSplash);
        tvCountingDown = (TextView) findViewById(R.id.tvCountingDown);
        timer = new Handler();
        final String type = FmHttpBiz.getType(getApplicationContext());
        if (YcApp.TYPE_BROKER.equals(type)) {
            FmHttpBiz.getSplashImage(this, ivSplash);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!hasRightInfo()) {
                    boolean isFirst = CommonSp.isFirst();
                    System.out.print("isFirst=" + isFirst);
                    if (isFirst) {
                        CommonSp.setFirst("1");
                        IntentHelper.getInstances().getIntent(SplashActivity.this, GuideFragmentActivity.class);
                    } else {
                        IntentHelper.getInstances().getIntent(SplashActivity.this, LoginActivity.class);
                    }
                } else {
                    if (type.equals(YcApp.TYPE_BROKER)) {//房米快销
                        IntentHelper.jump(SplashActivity.this, MainActivity.class, "type", "splash");
                    } else {//房米云
                        IntentHelper.getInstances().getIntent(SplashActivity.this, MainActivity.class);
                    }
                }
                SplashActivity.this.finish();
            }
        }, 2000);
        countingDown();
    }

    private boolean hasRightInfo() {
        if ("".equals(CommonSp.getUserInfo())) {
            return false;
        }
        return true;
    }

    private void countingDown() {
        timer.postDelayed(new Runnable() {
            @Override
            public void run() {
                count = count - 1;
                if (count > 0) {
                    tvCountingDown.setText(String.valueOf(count) + "S");
                    countingDown();//递归
                }
            }
        }, 1000);
    }
}
